package com.viroyal.monitorlib.network.socket;

import com.google.gson.Gson;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Created by LiGang on 2016/9/21.
 */

public class CustomMessageEncoder extends MessageToByteEncoder<Device2CloudMessage> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Device2CloudMessage msg, ByteBuf out) throws Exception {
        out.writeByte(1);
        String msgData = new Gson().toJson(msg);
        out.writeBytes(msgData.getBytes("utf-8"));
    }

    @Override
    public boolean isSharable() {
        return true;
    }
}
